package cn.donting.jme.expansion.builder.light;

import cn.donting.jme.EditorContent;
import cn.donting.jme.editor.scene.Scene;
import cn.donting.jme.editor.scene.wrapper.DirectionalLightWrapper;
import cn.donting.jme.editor.scene.wrapper.LightWrapper;
import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.ValueChangeListener;
import cn.donting.jme.expansion.attribute.lmpl.Vector3fAttribute;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.ioc.Sort;
import cn.donting.jme.utile.MathUtil;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.jme3.light.DirectionalLight;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;

import javax.swing.*;
import java.util.List;

@Bean
public class DirectionalLightAttributeBuilder extends LightAttributeBuilder<DirectionalLight> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof DirectionalLight;
    }

    @Override
    public DirectionalLight newInstance() {
        DirectionalLight directionalLight = new DirectionalLight();
        directionalLight.setName("方向光");
        directionalLight.setDirection(new Vector3f(-1,-2,-3));
        return directionalLight;
    }

    @Override
    public Icon getIcon(int size) {
        return new FlatSVGIcon("icon/directionalLight.svg",size,size);
    }

    @Override
    public Icon getActiveIcon(int size) {
        return new FlatSVGIcon("icon/directionalLight-active.svg",size,size);
    }

    @Override
    public String getName() {
        return "方向光";
    }

    @Override
    public void attribute(List<Attribute> attributes, DirectionalLight directionalLight) {
        super.attribute(attributes,directionalLight);
        Scene scene = EditorContent.getScene();
        DirectionalLightWrapper wrapper = scene.getWrapper(directionalLight, DirectionalLightWrapper.class);
        Quaternion rotation = wrapper.getRotation();
        Vector3f vector3f = MathUtil.quaternionToAngles(rotation);
        Vector3fAttribute directionAttribute  = new Vector3fAttribute();
        directionAttribute.setTitle("rotation");
        directionAttribute.setValue(vector3f);
        directionAttribute.setValueChangeListener(newValue -> {
            Quaternion quaternion = MathUtil.anglesToQuaternion(newValue);
            wrapper.setRotation(quaternion);
            directionalLight.setDirection(quaternion.getRotationColumn(2));
        });
        attributes.add(directionAttribute);
    }
}
